package com.hongdee.scm.carInvoicePrice.controller;

import com.hongdee.base.entity.ResultEntity;
import com.hongdee.scm.car.service.ITScmCarService;
import com.hongdee.scm.carInvoicePrice.entity.TScmCarInvoicePrice;
import com.hongdee.scm.carInvoicePrice.service.ITScmCarInvoicePriceService;
import com.hongdee.scm.invioce.service.ITCrmCarInvoiceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/7/13.
 */
@RestController
@RequestMapping("src/carInvoicePrice")
public class TScmCarInvoicePriceController {
    @Autowired
    private ITScmCarInvoicePriceService CarInvoicePriceService;

    @Autowired
    private ITScmCarService itScmCarService;
    @Autowired
    private ITCrmCarInvoiceService itCrmCarInvoiceService;

    /**
     * 价格单据查询
     *
     * @param uid
     * @param token
     * @throws Exception
     */

    @RequestMapping("/selectByInvoicePrice")
    @ResponseBody
    public List<Map> selectGetInvoicePrice(@RequestParam(name = "uid", defaultValue = "") String uid,
                                     @RequestParam(name = "token", defaultValue = "") String token,
                                     @RequestBody List<Map> list
                                     ) throws Exception {
        List<Map> map1 = new ArrayList<>();
        for (Map mlist:list
             ) {
            Map map = new HashMap();
            String carId = String.valueOf(mlist.get("carId"));
            String invoicesType = String.valueOf(mlist.get("invoicesType"));
            String invoicesPrice = String.valueOf(mlist.get("invoicesPrice"));
            map =this.CarInvoicePriceService.selectByinvoice(carId,invoicesType,uid,invoicesPrice);
            map1.add(map);
            itScmCarService.updateFinanceInfo(carId, invoicesType, invoicesPrice);
            //销售车辆匹配
            itCrmCarInvoiceService.refreshCar(uid,carId);

        }
        return map1;
    }

    /**
     * 批量打印
     * @param uid
     * @param token
     * @param list
     * @return
     * @throws Exception
     */
    @RequestMapping("/batchByInvoicePrice")
    @ResponseBody
    public List<Map> batchByInvoicePrice(@RequestParam(name = "uid", defaultValue = "") String uid,
                                         @RequestParam(name = "token", defaultValue = "") String token,
                                         @RequestBody List<Map> list
    ) throws Exception {

        List<Map> mapList = new ArrayList<>();
        List<String> carId = new ArrayList<>();
        String invoicesType ="";
        String invoicesPrice="";
        boolean flag = true;
        for (Map mlist:list
                ) {
            Map map = new HashMap();
            carId = (List<String>) mlist.get("carId");
            invoicesType = String.valueOf(mlist.get("invoicesType"));
            invoicesPrice = String.valueOf(mlist.get("invoicesPrice"));
            if( mlist.get("flag")!=null && !mlist.get("flag").equals("")){//入到上一个月最后一天
                flag = (boolean) mlist.get("flag");
            }
            map =this.CarInvoicePriceService.batchByinvoice(carId,invoicesType,uid,invoicesPrice,flag);
            //map =this.CarInvoicePriceService.batchByinvoice(carId,invoicesType,uid,invoicesPrice);
            mapList.add(map);
        }
        for (String id:carId){
            itScmCarService.updateFinanceInfo(id, invoicesType, invoicesPrice);
            //销售车辆匹配
            itCrmCarInvoiceService.refreshCar(uid,id);
        }

        return mapList;
    }

    /**
     * 打印完成更新数据
     *
     * @param uid
     * @param token
     * @throws Exception
     */

    @RequestMapping("/updateGetInvoicePrice")
    @ResponseBody
    public Map updateGetInvoicePrice(@RequestParam(name = "uid", defaultValue = "") String uid,
                                              @RequestParam(name = "token", defaultValue = "") String token,
                                              @RequestBody String[] ids) throws Exception {
        Map map = new HashMap();
        if(ids.length>0){
            for(String id :ids ) {
                this.CarInvoicePriceService.updateByinvoice(id);
            }
        }else {
            throw new Exception("请选择单据！");
        }
        map.put("success",true);
        return map;
    }

    /**
     * 打印成本计价单
     *
     * @param uid
     * @param token
     * @throws Exception
     */
    @RequestMapping("/saveGetInvoicePrice")
    @ResponseBody
    public ResultEntity saveGetInvoicePrice(@RequestParam(name = "uid", defaultValue = "") String uid,
                                   @RequestParam(name = "token", defaultValue = "") String token,
                                   @RequestBody TScmCarInvoicePrice invoicePrice) throws Exception{
        ResultEntity res=new ResultEntity();
        this.CarInvoicePriceService.saveCarInvoicePrice(invoicePrice);
        res.setMsg("保存成功！");
        res.setSuccess(true);
        res.setData(invoicePrice);
        return res;
    }

    /**
     * 打印入库单修改财务状态
     *
     * @param uid
     * @param token
     * @throws Exception
     */
    @RequestMapping("/updatePriceTye")
    @ResponseBody
    public ResultEntity updatePriceTye(@RequestParam(name = "uid", defaultValue = "") String uid,
                                            @RequestParam(name = "token", defaultValue = "") String token,
                                       @RequestParam(name = "invoicesType", defaultValue = "") String invoicesType,
                                        @RequestParam(name = "carId", defaultValue = "" ) String carId) throws Exception{
        ResultEntity res=new ResultEntity();
        this.CarInvoicePriceService.updateByinvoiceType(invoicesType,carId);
        res.setMsg("更新成功！");
        res.setSuccess(true);
        return res;
    }

    /**
     * 车辆价格历史明细
     * @param uid
     * @param carId
     * @return
     * @throws Exception
     */
    @RequestMapping("/getHistoryPrice")
    @ResponseBody
    public Map getHistoryPrice(@RequestParam(name = "uid", defaultValue = "") String uid,
                               @RequestParam(name = "carId", defaultValue = "") String carId,
                               @RequestParam(name = "start", defaultValue = "0") String start,
                               @RequestParam(name = "limit", defaultValue = "50") String limit
    ) throws Exception{

        Map map = new HashMap();
        map= this.CarInvoicePriceService.getHistoryPrice(carId,uid,start,limit);
        map.put("success","查询成功!");
        return map;
    }

    /**
     * 维护已入库车辆的暂估价格
     * @param uid
     * @param token
     * @return
     * @throws Exception
     */
    @RequestMapping("/updateEstimatePrice")
    @ResponseBody
    public ResultEntity updateEstimatePrice(@RequestParam(name = "uid", defaultValue = "") String uid,
                                         @RequestParam(name = "token", defaultValue = "") String token,
                                         @RequestParam (name = "id", defaultValue = "") String id,
                                         @RequestParam (name = "price", defaultValue = "") String price
    ) throws Exception {
        String  type = "1";
        this.CarInvoicePriceService.updateEstimatePrice(id,type,uid,price);
        ResultEntity res=new ResultEntity();
        res.setMsg("更新成功！");
        res.setSuccess(true);
        return res;
    }

}
